# -*- coding:utf-8 -*-
# editor: zzh

import json
import re
import pickle

def sort_by_value(d):
    items=d.items()
    backitems=[[v[1],v[0]] for v in items]
    backitems.sort()
    return {backitems[i][1]:d[backitems[i][1]] for i in range(-1,-len(backitems)-1,-1)}

datas = json.load(open('data_label.json','r',encoding='utf8'))
print(len(datas))

intent2ents = {}

enttype2id = pickle.load(open('type2dict.pkl','rb'))

for dd in datas:
    # print(dd['id'])
    for d in dd['content']:
        try:
            infos = d['info']
            ents = infos['ents']
            triples = infos['triples']
            # print(d)

            sentences = (d['[SPEAKER 1]'], d['[SPEAKER 2]'])
            intent1 = d['客服意图']
            intent2 = d['用户意图']

            # print(ents)
            intent1 = intent1.replace(" ", "")
            intent1 = intent1.replace("（", "(")
            intent1 = intent1.replace("）", ")")
            intent1 = re.sub(r'[(](.*)', '', intent1)

            intent1s = re.split(',|-|，|=|\*|—', intent1)
            for i1 in intent1s:
                i1 = i1.replace('·', '')
                i1 = i1.replace(')', '')
                # print(i1)
                if i1 not in intent2ents:
                    intent2ents[i1] = {}

                #
                for tri in triples:
                    if tri['pos'][0] == 1 and tri['prop'] not in intent2ents[i1]:
                        # intent2ents[i1].append(tri['prop'])
                        intent2ents[i1][tri['prop']] = 0
                    intent2ents[i1][tri['prop']] += 1

    ############################################################
            intent2 = intent2.replace(" ", "")
            intent2 = intent2.replace("（", "(")
            intent2 = intent2.replace("）", ")")
            intent2 = re.sub(r'[(](.*)', '', intent2)

            intent2s = re.split(',|-|，|=|\*|—', intent2)
            for i1 in intent2s:
                i1 = i1.replace('·', '')
                i1 = i1.replace(')', '')
                # print(i1)
                if i1 not in intent2ents:
                    intent2ents[i1] = {}

                # for ent in ents:
                #     poses = ent['pos']
                #     for pos in poses:
                #         if (pos[0] == 1 and ent['type'] not in intent2ents[i1]):
                #             # intent2ents[i1].append(ent['type'])
                #             intent2ents[i1][ent['type']] = 0
                #         intent2ents[i1][ent['type']] += 1

                for tri in triples:
                    if (tri['pos'][0] == 2 and tri['prop'] not in intent2ents[i1]):
                        # intent2ents[i1].append(tri['prop'])
                        intent2ents[i1][tri['prop']] = 0
                    intent2ents[i1][tri['prop']] += 1

        except Exception as e:
            pass


intent2ents = {k:sorted(intent2ents[k]) for k in intent2ents if len(intent2ents[k]) >= 3}
intent2ents.pop('')
# print(len(intent2ents))
# for k in intent2ents:
#     print(k,len(intent2ents[k]),sort_by_value(intent2ents[k]))
#
print(len(intent2ents.keys()))
print(intent2ents.keys())
pickle.dump(intent2ents, open('intent2prop_24.pkl','wb'))